package com.rpframework.website.device.act.api;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.rpframework.core.utils.TagUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.act.admin.AdminAct;
import com.rpframework.website.device.domain.Reportactivity;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.ReportactivityService;


@Controller
@RequestMapping("api/report")
public class ApiReportAct  extends AdminAct{
	Gson gson =new Gson();
	@Resource ReportactivityService reportactivityService;
	
	
	//查找我的投诉的
		@RequestMapping(value="/list" ,produces = "application/json; charset=utf-8")
		public @ResponseBody String list(@RequestParam(value="pager",required = false )Pager<Reportactivity> pager,
				@RequestParam(value = "callback",required = false ) String callback,
				HttpSession session){
			if(pager==null){
				pager=new Pager<Reportactivity>();
			}
			User sessionuser=getSessionUser(session);
			if(sessionuser==null){
				throw new APICodeException(-4, "你还没登陆!");
			}
			pager.getSearchMap().put("userid", sessionuser.getId()+"");
			pager=reportactivityService.sepager(pager);
			JsonObject json=new JsonObject();
			json.addProperty("totalPages", pager.getTotalPages());
			json.addProperty("currentPage", pager.getCurrentPage());
			json.addProperty("totalCount", pager.getTotalCount());
			List<Reportactivity> list=pager.getItemList();
			JsonArray array=new JsonArray();
			json.add("arrays", array);
			for(Reportactivity act:list){
				JsonObject jsonObj = gson.toJsonTree(act).getAsJsonObject();
				jsonObj.addProperty("acttime", TagUtils.formatDate(act.getActtime()));
				array.add(jsonObj);
			}
			return (callback + "(" + json + ")");
		}
				/**
				 * 投诉建议  投诉填写 时间不对
				 * @param phone
				 * @param reportContext
				 * @param name
				 * @param tillet
				 * @param callback
				 * @param session
				 * @return
				 * @time 2016年3月8日 下午8:47:40
				 */
				@RequestMapping(value="/addreport" ,produces = "application/json; charset=utf-8")
				public @ResponseBody String addrepair(
						@RequestParam String phone,
						@RequestParam String reportContext,
						@RequestParam String name,
						@RequestParam String tillet,
						@RequestParam(value = "callback",required = false ) String callback,
						HttpSession session){
					
					User sessionuser=getSessionUser(session);
					if(sessionuser==null){
						throw new APICodeException(-4, "你还没登陆!");
					}
					Reportactivity t=new Reportactivity();
					t.setPhone(phone);
					t.setName(name);
					t.setReportContext(reportContext);
					t.setTeltile(tillet);
					t.setUserid(sessionuser.getId());
					t.setPropertyid(sessionuser.getEstateId());
					t.setActtime(System.currentTimeMillis()/1000);
					boolean bfgl=reportactivityService.insertdo(t);
					
					JsonObject json=new JsonObject();
					if(bfgl){
						json.addProperty("succ", true);
					}else{
						json.addProperty("succ", false);
					}
				
					return (callback + "(" + json + ")");
				}
}
